<?php

include_once("_include/client/facebook.php");
include_once("FacebookMock.php");
include_once("data/dao/CollectionDAO.php");
include_once("data/dao/GameDAO.php");
include_once("data/dao/WishListDAO.php");
include_once("data/dao/FriendCodeDAO.php");
include_once("application/game/OwnedGame.php");
include_once("application/game/Game.php");
include_once("utils/ClassUtils.php");
include_once("application/WishListUtils.php");
include("conf.php");

/**
 * @version versionstring
 */
class FacebookProxy{
	
	private $facebook;
	
	public function FacebookProxy(){
		
		// this allows local testing, with a mock class
		if(ENVIRONMENT == "testing")
			$this->facebook = new FacebookMock();
		else
			$this->facebook = new Facebook(FACEBOOK_API_KEY, FACEBOOK_SECRET_KEY);
	}
	
	public function requireFrame(){
		
		$this->facebook->require_frame();
	}

	public function requireLogin(){

		$user = $this->facebook->require_login();
		
		$appcallbackurl = 'http://wiisocial.mattmakins.co.uk';
		
		try {
  			if (!$this->facebook->api_client->users_isAppAdded()) {
    			$this->facebook->redirect($this->facebook->get_add_url());
  			}
		} 
		catch (Exception $ex) {
  			//this will clear cookies for your application and redirect them to a login prompt
  			$this->facebook->set_user(null, null);
  			$this->facebook->redirect($appcallbackurl);
		}
		
		return $user;
	}
	
	public function getLoggedInUser(){
		
		return $this->facebook->get_loggedin_user();
	}
	
	public function publishStory($title, $body, $image){
		
	}
	
	public function getFacebookUrl(){
		return $this->facebook->get_facebook_url();
	}
	
	public function updateProfile(){
	
		$html = $this->getCollectionHTML();
		$html = $html . $this->getWishListHTML();		
		$this->facebook->api_client->profile_setFBML($html, $this->getLoggedInUser());
	}
	
	private function getCollectionHTML(){
		
		$cdao = new CollectionDAO();
		$collection = $cdao->getCollection($this->getLoggedInUser());
		
		if($collection == null)
			return;
			
		$collection = $collection->getGames();	
		$gdao = new GameDAO();
		$udao = new UserDAO();
		
		$tdCount = 0;			
		$html = "<fb:subtitle>" .
				  "<fb:action href='http://apps.facebook.com/wiisocial//games.php'>Add Games</fb:action>" .
				  "<fb:action href='http://apps.facebook.com/wiisocial//topGames.php'>Top Games</fb:action>" .
				  "<fb:action href='http://www.facebook.com/apps/application.php?id=6391538278'>Want this application?</fb:action>" .
				"</fb:subtitle>" .	
				"<span style='font-family: verdana; color: #808080;'>Wii Code:</span>&nbsp;&nbsp;" .
		        $udao->findUser($this->getLoggedInUser())->getWiiCode() . 
		        "<br/><br/>" .
				"<span style='font-family: verdana; color: #808080;'>Owned games:</span>" .
				"<br/>" .
				"<table align='center'>" .
				"<tr>";
		foreach($collection as $g){
			
			// convert game to an owned game to get and set friend code
			$game = ClassUtils::typecast($gdao->getGame($g), "OwnedGame");

			$fcdao = new FriendCodeDAO();
			if($game != null)
				$game->setFriendCode($fcdao->findFriendCode($this->getLoggedInUser(), $g));

			$html = $html .	
					"<td style='text-align: center;";
					if($game->getFriendCode()){
						$html = $html . " background-color: #FFFFCC; border: 1px solid #b7b7b7;";
					}
					$html = $html . "' align='center' width='60' height='90'>" .
					"<a href='http://wiisocial.mattmakins.co.uk/external.php?url=" . 
						$game->getAmazonAssociateURL() . "&game-id=" . 
						$game->getID() . "' target='_blank'><img width='50' height='75' src='" . $game->getImageURL() . "' title='" . $game->getTitle() . "'/></a><br/>";
			
			if($game->getFriendCode()){
				$html = $html . "<span style='font-family: verdana; font-size: 10px; color: #808080;'>Friend Code:</span><br/>" .
				"<span style='font-size:10px; text-align= center'>" . $game->getFriendCode() . "</span>";
			}
			$html = $html . "</td>";
					
			if($tdCount > 4){
				$html = $html . "</tr><tr>";
				$tdCount = 0;
			}

			$tdCount+=1;
		}
		$html = $html . "</tr></table><br/>";
		return $html;
	}
	
	private function getWishListHTML(){
		
		if(WishListUtils::wishListIsEmpty($this->getLoggedInUser()))
			return;

		$wldao = new WishListDAO();	
		$gdao = new GameDAO();
		$wishList = $wldao->getWishList($this->getLoggedInUser());
		
		$html = "<span style='font-family: verdana; color: #808080;'>Wish List:</span>&nbsp;&nbsp;";
		foreach($wishList as $g){
			
			$game = $gdao->getGame($g);
			$html = $html . '<a href="http://wiisocial.mattmakins.co.uk/external.php?url=' . 
				$game->getAmazonAssociateURL() . '&game-id=' . $game->getID() . '" target="_blank">' .
					$game->getTitle() . "</a>, ";
		}
		return $html;
	}
	
	/**
	 * returns the following information for the passed
	 * facebook user ids
	 * 
	 * uid
	 * first_name
	 * last_name
	 * has_added_app
	 * pic_square
	 * pic
	 * 
	 * 
	 */
	public function getFacebookUserInfo($userId){
		
		$info =  $this->facebook->api_client->users_getInfo($userId, array('uid', 
			'first_name', 'last_name', 'has_added_app', 'pic_square', 'pic'));
		return $info;
	}
/*	
	public function isAppAdded($userId){
		return $this->facebook->api_client->users_getInfo($userId, 'is_app_added');
	}
*/	
	/*
	public function getUserProfilePicUrl($userId){
		
		$url = $this->facebook->api_client->users_getInfo($userId, 'pic');
		return $url[0]['pic'];
	}
	*/

	public function sendEmailNotification($users, $subject, $body){
		
		$this->facebook->api_client->notifications_sendEmail($users, $subject, $body);
	}
	
	public function areFriends($user, $friend){
		
		$fb = $this->facebook->api_client->friends_areFriends($user, $friend);
		return $fb[0]['are_friends'];
	}
}
?>
